import axios from 'axios'

axios.defaults.baseURL = 'http://www.520gongfu.com:9527/api'

axios.interceptors.request.use(function(config) {
    // 设置 请求 token 校验
    const token = sessionStorage.getItem('token')
    if (token) {
        config.headers['Authorization'] = token
    }
    // 在发送请求之前做些什么
    return config;
}, function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
})

export const getSlides = (callback) => {
    axios.get("/home/sliders").then(function(res) {
        // console.log(res);
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

// /home/category
export const homeCategory = (callback) => {
    axios.get("/home/category").then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

// /home/floorlist
export const homeFloorlist = (callback) => {
    axios.get("/home/floorlist").then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

// /category/data

export const getCategory = (callback) => {
    axios.get('/category/data').then(res => {
        console.log(res);
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

import qs from 'qs'
// /user/login
export const Dologin = (params, callback) => {
    axios.post("/user/login", qs.stringify(params)).then(res => {
        // console.log(res);
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

// /user/info
export const getuserInfo = (callback) => {
    axios.get("/user/info").then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

//  /cart/list
export const getCarInfo = (callback) => {
    axios.get("/cart/list").then(res => {
        callback && callback(res.data)
    })
}

// /product/recommend

export const proRec = (callback) => {
    axios.get("/product/recommend").then(res => {
        callback && callback(res)
    }).catch(err => {
        console.log(err);
    })
}

// 添加 购物车商品
export const carAdd = (params, callback) => {
    axios.post('/cart/add', qs.stringify(params), {
        headers: {
            Authorization: localStorage.getItem('token')
        }
    }).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log('接口异常' + err.message)
    })
}

// cart/del
export const carDel = (params, callback) => {
    axios.post("/cart/del", qs.stringify(params)).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

// product/detail
export const getDetail = (params, callback) => {
    axios.get(`/product/detail?id=${params}`).then(res => {
        callback && callback(res)
    }).catch(err => {
        console.log(err);
    })
}

// user/logout
export const loginOut = (callback) => {
    axios.post("/user/logout").then(res => {
        callback && callback(res)
    }).catch(err => {
        console.log(err);
    })
}

// user/qqinfo
export const QQInfo = (params, callback) => {
    axios.get("user/qqinfo", {
        params: params,
        paramsSerializer: function(params) {
            return qs.stringify(params)
        }
    }).then(res => {
        callback && callback(res)
    }).catch(err => {
        console.log(err);
    })
}

// user/qqlogin

export const getqqUser = (params, callback) => {
    axios.post("/user/qqlogin", qs.stringify(params)).then(res => {
        callback && callback(res)
    }).catch(err => {
        console.log(err);
    })
}